/**
 * 对比用户线程和守护线程的区别
 */
public class ThreadDemoByDaemon2 {
    public static void main(String[] args) throws InterruptedException {
        userThread();
        daemonThread();
        //Thread.sleep(1000);
    }

    //守护线程
    private static void daemonThread() {
        Thread thread=new Thread(()->{
            for (int i = 0; i <10 ; i++) {
               System.out.println("守护线程执行:"+i);
                try {
                    Thread.sleep(850);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //设置线程为守护线程
        thread.setDaemon(true);
        //启动线程
        thread.start();
    }

    //用户线程
    private static void userThread() {
        Thread thread=new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                System.out.println("用户线程执行:"+i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }
}
